package com.stonedt.spider.controller.spider;

import com.stonedt.spider.aop.AuthorizationInterceptor;
import com.stonedt.spider.aop.Token;
import com.stonedt.spider.controller.BaseController;
import com.stonedt.spider.entity.SpiderFlowTemplate;
import com.stonedt.spider.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/template")
public class TemplateController extends BaseController {

    @Autowired
    private TemplateService templateService;


    @Token
    @GetMapping("/page")
    public Object template_page(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                @RequestParam(value = "pageSize",required = false,defaultValue = "10")int pageSize,
                                @RequestParam(value = "environment",required = false)Integer environment,
                                @RequestParam(value = "status",required = false)Integer status){
        try {
            if(pageNum>0 && pageSize>0){
                int beginIndex = (pageNum-1)*pageSize;
                String limit = "limit "+beginIndex+","+pageSize;
                return success_int(200,"查询成功!",templateService.templatePage(limit,environment,status));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return error_int(500,"查询失败, 请检查参数是否有误！","");
    }

    @Token
    @GetMapping("/list")
    public Object template_list(){
        try {
            List<Map<String,Object>> list = templateService.templates();
            return success_int(200,"查询成功!",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return error_int(500,"查询失败, 请检查参数是否有误！","");
    }

    @Token
    @GetMapping("/info")
    public Object template_info(@RequestParam(value = "id",required = false,defaultValue = "0")int id){
        try {
            if(id>0 ){
                return success_int(200,"查询成功!",templateService.templateInfo(id));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return error_int(500,"查询失败, 请检查参数是否有误！","");
    }

    @Token
    @GetMapping("/open")
    public Object template_open(@RequestParam(value = "id",required = false,defaultValue = "0")int id,
                                @RequestParam(value = "status",required = false)Integer status,
                                HttpServletRequest request){
        try {
            String userInfo = request.getAttribute(AuthorizationInterceptor.USER_INFO).toString();
            int userId = getUserId(userInfo);
            String userName = getUserName(userInfo);
            if(id>0 && status!=null && userId>0 && userName!=null){
                int type = templateService.openTemplate(id,status,userId,userName);
                if(type>0){
                    return success_int(200,"更新成功!",type);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return error_int(500,"更新失败, 请检查参数是否有误！","");
    }

    @Token
    @PostMapping("/save")
    public Object template_save(@RequestBody SpiderFlowTemplate template,
                                HttpServletRequest request){
        try {
            String userInfo = request.getAttribute(AuthorizationInterceptor.USER_INFO).toString();
            int userId = getUserId(userInfo);
            String userName = getUserName(userInfo);
            if(template!=null  && userId>0 && userName!=null ){
                template.setUpdate_user_id(userId);
                template.setUpdate_user_name(userName);
                int type = 0;
                if(template.getId()==0 && template.getType_id()!=null){
                    if( template.getTemplate_name()!=null){
                        template.setCreate_user_id(userId);
                        template.setCreate_user_name(userName);
                        type = templateService.saveTemplate(template);
                    }
                }else {
                    type = templateService.updateTemplate(template);
                }
                if(type>0 ) {
                    return success_int(200,"保存成功!",type);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return error_int(500,"保存失败, 请检查参数是否有误！","");
    }

//    @Token
//    @PostMapping("/update")
//    public Object template_update(@RequestBody SpiderFlowTemplate template,
//                                  HttpServletRequest request){
//        try {
//            String userInfo = request.getAttribute(AuthorizationInterceptor.USER_INFO).toString();
//            int userId = getUserId(userInfo);
//            String userName = getUserName(userInfo);
//            if(template!=null && template.getId()>0 && userId>0 && userName!=null  ){
//                template.setUpdate_user_id(userId);
//                template.setUpdate_user_name(userName);
//                int type = templateService.updateTemplate(template);
//                if(type>0){
//                    return success_int(200,"更新成功!",type);
//                }
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return error_int(500,"更新失败, 请检查参数是否有误！","");
//    }

}
